package boot.spring.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class httpTest {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://lotsmg.dpm.org.cn/tyApi/zyborder/order/saveUnpaidOrder");
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("POST");

        httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

        httpConn.setRequestProperty("tyAuthToken", "59F644B514154B5FB34EE26BD20B04C3");

        httpConn.setDoOutput(true);
        long currentTimeMillis = System.currentTimeMillis();

        //String data = "[{\"goodsCode\":\"TYMP20230324005380\",\"ticketId\":1178,\"quantity\":1,\"occDate\":\"2024-09-07\",\"isGiftTicket\":\"F\",\"occStartTime\":\"08:30\",\"occEndTime\":\"16:10\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"513922199812163691\",\"realName\":\"黄飞龙\"}]},{\"goodsCode\":\"TYMP20220708005333\",\"quantity\":1,\"occDate\":\"2024-09-07\",\"isGiftTicket\":\"F\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"130803198007280200\",\"realName\":\"王慧\",\"touristType\":\"\"}]}]";
        //String data = "[{\"goodsCode\":\"TYMP20230324005383\",\"ticketId\":1179,\"quantity\":1,\"occDate\":\"2024-09-13\",\"isGiftTicket\":\"F\",\"occStartTime\":\"08:30\",\"occEndTime\":\"16:10\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"513922199812163691\",\"realName\":\"黄飞龙\"}]},{\"goodsCode\":\"TYMP20220708005333\",\"quantity\":1,\"occDate\":\"2024-09-13\",\"isGiftTicket\":\"F\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"130803198007280200\",\"realName\":\"王慧\",\"touristType\":\"\"}]}]";
        String data = "[{\"goodsCode\":\"TYMP20220704005220\",\"ticketId\":1035,\"quantity\":1,\"occDate\":\"2024-09-14\",\"isGiftTicket\":\"F\",\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"140122199405301115\",\"realName\":\"董鑫\"}]}]";
        //String data = "[{\"goodsCode\":\"TYMP20220704005220\",\"ticketId\":1035,\"quantity\":10,\"occDate\":\"2024-09-13\",\"isGiftTicket\":\"F\",\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"350981199309100041\",\"realName\":\"雷文文\"},{\"authType\":\"IC\",\"certNo\":\"352202199202295414\",\"realName\":\"王振良\"},{\"authType\":\"IC\",\"certNo\":\"352226197101305427\",\"realName\":\"钟荣妹\"},{\"authType\":\"IC\",\"certNo\":\"352202197402015422\",\"realName\":\"钟美珠\"},{\"authType\":\"IC\",\"certNo\":\"352226197003165731\",\"realName\":\"雷石梅\"},{\"authType\":\"IC\",\"certNo\":\"352226197101115463\",\"realName\":\"郑淑英\"},{\"authType\":\"IC\",\"certNo\":\"352226196811251019\",\"realName\":\"刘平\"},{\"authType\":\"IC\",\"certNo\":\"352226197208205127\",\"realName\":\"钟团秀\"},{\"authType\":\"IC\",\"certNo\":\"352202197312075114\",\"realName\":\"钟团富\"},{\"authType\":\"IC\",\"certNo\":\"352201198007165429\",\"realName\":\"苏秋菊\"}]}]";
        String sign = encryptOrder(data,currentTimeMillis);
        OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
        //String json = "{\"totalPrice\":\"500\",\"goodsList\":"+data+",\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725683992894,\"sign\":\"7e0ee487fefd080855047fe650c1cfe0a04663ffc21fb1d4bb0f8508daf5e83f718f7bcc357cd42659c8cc2f6be5afd6\"}";
        // String json = "{\"totalPrice\":\"60000\",\"goodsList\":"+data+",\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725686010916,\"sign\":\"3d640c44c6f408a9281d297966f07ec13e2e150bc3832208018275d19983cf7d80fa07797166fe91b34b113857e6a835\"}";
//        String json  = "{\"totalPrice\":\"500\",\"goodsList\":"+data+",\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725775494526,\"sign\":\"5cedbec104c4347cb5f560583bbdee195231930e69bc62c0ef78174e4c66a37bf2edf1d762937d47af36ab34c2c269eb\"}";
        //String json = "{\"totalPrice\":\"6000\",\"goodsList\":"+data+",\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725776949500,\"sign\":\"fe995ab94f1810e5f39a46158d1a8c6c6b1466c743c494d0d3445d8e2ee28c49fd270cb595e8b6acef2a57e6efa6392e\"}";
        //String json = "{\"totalPrice\":\"6000\",\"goodsList\":"+data+",\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725777289957,\"sign\":\"fe995ab94f1810e5f39a46158d1a8c6c6b1466c743c494d0d3445d8e2ee28c4933a62d8c183e94a0a41844d08eb4e1aa\"}";

        JSONObject json1 = JSON.parseObject("{\"totalPrice\":\"6000\",\"goodsList\":[{\"goodsCode\":\"TYMP20220704005220\",\"ticketId\":1035,\"quantity\":1,\"occDate\":\"2024-09-14\",\"isGiftTicket\":\"F\",\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"customerInfos\":[{\"authType\":\"IC\",\"certNo\":\"140122199405301115\",\"realName\":\"董鑫\"}]}],\"contactPerson\":{\"linkName\":\"王慧\",\"linkTel\":\"13633243055\",\"certNo\":\"130803198007280200\",\"authType\":\"IC\"},\"saveOrderFlag\":\"F\",\"scenicId\":242,\"scenicName\":\"故宫博物院\",\"timesTamp\":1725777289957,\"sign\":\"fe995ab94f1810e5f39a46158d1a8c6c6b1466c743c494d0d3445d8e2ee28c4933a62d8c183e94a0a41844d08eb4e1aa\"}");


        System.out.println(currentTimeMillis);
        json1.put("timesTamp",currentTimeMillis);
        json1.put("sign",sign);
        System.out.println(json1.toJSONString());
        writer.write(json1.toJSONString());
        writer.flush();
        writer.close();
        httpConn.getOutputStream().close();

        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        System.out.println(response);
    }

    private static String encryptOrder(String str, long time) throws IOException {
        String params = "group=test_web&action=getEncryptSign&l="+str+"&m="+time+"&bind_client=0.192903304593127";

//                client2;
        URL url = new URL("http://127.0.0.1:5612/business-demo/invoke");
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpConn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
        writer.write(params);
        writer.flush();
        writer.close();
        httpConn.getOutputStream().close();
        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        System.out.println(response);
        JSONObject json = JSON.parseObject(response);
        return json.getString("data");
    }
}
